package com.zzl.bonds.ticket.service.common.impl;

import com.zzl.bonds.ticket.constant.StringConstant;
import com.zzl.bonds.ticket.constant.status.TicketStatusEnum;
import com.zzl.bonds.ticket.constant.type.OperateButtonEnum;
import com.zzl.bonds.ticket.dto.KeyValue;
import com.zzl.bonds.ticket.service.common.MyTicketHandStatusService;
import com.zzl.bonds.ticket.untils.TicketUtils;
import com.zzl.general.common.exception.SysException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的票据 对 处理状态和 操作列 的 统一处理 方法
 *
 * @author : spp
 * @date : 2022/2/23 20:58
 */
@Service
public class MyTicketHandStatusServiceImpl implements MyTicketHandStatusService {

    /**
     * 设置我的票据---平台 的操作按钮
     *
     * @param ticketList 票对象啊
     */
    @Override
    public void setMyTicketAllOperates(List<?> ticketList) throws SysException {
        for (Object ticket : ticketList) {
            List<KeyValue> keyValues = new ArrayList<>();
            Integer code = TicketUtils.getTicketStatus(ticket).getCode();
            // 拿到冻结状态
            Integer frozenState = TicketUtils.getFrozenState(ticket);
            keyValues.add(new KeyValue(OperateButtonEnum.DETAILS.getCode(), OperateButtonEnum.DETAILS.getValue()));
            if (TicketStatusEnum.NORMAL_HOLDING.getCode().equals(code)) {
                if (frozenState.equals(StringConstant.NO)) {
                    keyValues.add(new KeyValue(OperateButtonEnum.FRAOZEN.getCode(), OperateButtonEnum.FRAOZEN.getValue()));
                } else {
                    keyValues.add(new KeyValue(OperateButtonEnum.NOT_FRAOZEN.getCode(), OperateButtonEnum.NOT_FRAOZEN.getValue()));
                }
            }
            TicketUtils.setOperateButton(ticket, keyValues);
        }
    }

    /**
     * 设置我的票据---我持有的 的操作按钮
     *
     * @param ticketList 票对象啊
     */
    @Override
    public void setMyTicketHoldOperates(List<?> ticketList) throws SysException {
        for (Object ticket : ticketList) {
            List<KeyValue> keyValues = new ArrayList<>();
            Integer code = TicketUtils.getTicketStatus(ticket).getCode();
            // 拿到冻结状态
            Integer frozenState = TicketUtils.getFrozenState(ticket);
            keyValues.add(new KeyValue(OperateButtonEnum.DETAILS.getCode(), OperateButtonEnum.DETAILS.getValue()));
            if (TicketStatusEnum.NORMAL_HOLDING.getCode().equals(code) && frozenState.equals(StringConstant.NO)) {
                keyValues.add(new KeyValue(OperateButtonEnum.TICKET_TRANSFER.getCode(), OperateButtonEnum.TICKET_TRANSFER.getValue()));
                keyValues.add(new KeyValue(OperateButtonEnum.TICKET_FINANCE.getCode(), OperateButtonEnum.TICKET_FINANCE.getValue()));
            }
            TicketUtils.setOperateButton(ticket, keyValues);
        }
    }

    /**
     * 设置我的票据---转让中 的操作按钮
     *
     * @param ticketList 票对象啊
     */
    @Override
    public void setMyTicketTransferIngOperates(List<?> ticketList) throws SysException {
        for (Object ticket : ticketList) {
            List<KeyValue> keyValues = new ArrayList<>();
            KeyValue keyValue = new KeyValue(OperateButtonEnum.DETAILS.getCode(), OperateButtonEnum.DETAILS.getValue());
            Integer code = TicketUtils.getTicketStatus(ticket).getCode();
            if (TicketStatusEnum.TRANSFER_IN_LIST.contains(code)) {
                keyValues.add(new KeyValue(OperateButtonEnum.CANCEL_APPLY.getCode(), OperateButtonEnum.CANCEL_APPLY.getValue()));
            }
            keyValues.add(keyValue);
            TicketUtils.setOperateButton(ticket, keyValues);
        }
    }


    /**
     * 设置我的票据---融资中 的操作按钮
     *
     * @param ticketList 票对象啊
     */
    @Override
    public void setMyTicketFinanceIngOperates(List<?> ticketList) throws SysException {
        for (Object ticket : ticketList) {
            List<KeyValue> keyValues = new ArrayList<>();
            KeyValue keyValue = new KeyValue(OperateButtonEnum.DETAILS.getCode(), OperateButtonEnum.DETAILS.getValue());
            Integer code = TicketUtils.getTicketStatus(ticket).getCode();
            if (TicketStatusEnum.FINANCE_ING_LIST.contains(code)) {
                keyValues.add(new KeyValue(OperateButtonEnum.CANCEL_APPLY.getCode(), OperateButtonEnum.CANCEL_APPLY.getValue()));
            }
            keyValues.add(keyValue);
            TicketUtils.setOperateButton(ticket, keyValues);
        }
    }

    /**
     * 设置我的票据---失效 的操作按钮
     *
     * @param ticketList 票对象啊
     */
    @Override
    public void setMyTicketInvalidIngOperates(List<?> ticketList) throws SysException {
        for (Object ticket : ticketList) {
            List<KeyValue> keyValues = new ArrayList<>();
            KeyValue keyValue = new KeyValue(OperateButtonEnum.DETAILS.getCode(), OperateButtonEnum.DETAILS.getValue());
            keyValues.add(keyValue);
            TicketUtils.setOperateButton(ticket, keyValues);
        }
    }
}
